//
//  TopRateCell.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/11/21.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit


/// 每行几个
let kTopRateCellRowOfline = 2
let kTopRateCellImageRatio = CGFloat(110) / 163

let kTopRateCellSubCelllwidth = CGFloat(kScreenWidth - kEdgeDistance * 2 - (kTopRateCellRowOfline - 1) * kCCellInteritemSpacing) / CGFloat(kTopRateCellRowOfline)

let kTopRateCellSubCellheight = kTopRateCellSubCelllwidth * kTopRateCellImageRatio + 64

class TopRateCell: BaseCell {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    var hotelDetailAction:(()->Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()

        setupUI()
    }
    
    private func setupUI() {
        
        configCell(.container)
        collectionView.backgroundColor = kBgColor
        
        layout.itemSize = CGSize(width: kTopRateCellSubCelllwidth, height: kTopRateCellSubCellheight)
        layout.minimumInteritemSpacing = CGFloat(kCCellInteritemSpacing)
        layout.minimumLineSpacing = CGFloat(kCCellLineSpacing)
        let edge = CGFloat(kEdgeDistance)
        layout.sectionInset = UIEdgeInsets(top: 0, left: edge, bottom: edge, right: edge)
        
        collectionView.register(UINib(nibName: "TopRateSubCCell", bundle: nil), forCellWithReuseIdentifier: "TopRateSubCCell")
        
        collectionView.register(UINib(nibName: "FuncFootCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "FuncFootCollectionReusableView")
    }
    
    class func cellHeight() ->CGFloat {
        
        let lines = ceil(4.0 / CGFloat(kTopRateCellRowOfline))
        
        return kTopRateCellSubCellheight * lines + (lines - 1) * CGFloat(kCCellLineSpacing) + CGFloat(kEdgeDistance) + 42
    }
}

extension TopRateCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopRateSubCCell", for: indexPath) as! TopRateSubCCell
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionView.elementKindSectionFooter {
            
            let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FuncFootCollectionReusableView", for: indexPath) as! FuncFootCollectionReusableView
            
            view.funActioned = {
                
                print("----------")
            }
            
            return view
        }
        
        return UICollectionReusableView()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        
        return CGSize(width: kScreenWidth, height: 42)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        hotelDetailAction?()
    }
}
